<style scoped>
.storeReturn{
  margin-right: 15px;
  padding: 0 15px;
  background-color: white;
  height: 87.5vh;
  position: relative;
}
.od-btn{
  padding: 15px 0;
}
.od-tab{
  display: flex;
}
.tab-left{
  width: 30%;
  padding-right: 15px;
  border-right: 2px solid #f4f4f4;
}
.tab-right{
  padding-left: 15px;
  width: 70%;
}
.footView span{
    float: left;
    margin-right: 48px;
    line-height: 6vh;
    font-size: 14px;
  }
  .footView span i{
    margin-right: 5px;
  }
</style>

<style>
.storeReturn .el-checkbox.is-bordered {
  padding: 4px 10px;
  height: 30px;
}
</style>

<template>
  <div class="storeReturn">
    <div class="od-btn">
      <el-button :disabled="isRegular" @click="()=>{this.showNew=true,this.title='新建'}" icon="el-icon-plus" type="primary">新建</el-button>
      <el-button icon="el-icon-circle-check" @click="Confirm" :disabled="isShow || isRegular">确认</el-button>
      <!-- <el-button @click="inventory" icon="el-icon-box">退货</el-button> -->
      <el-button :disabled="isRegular" @click="print" icon="el-icon-printer">列印</el-button>
      <el-button @click="Delete" icon="el-icon-delete" :disabled="isShow || isRegular">删除</el-button>
      <el-button icon="el-icon-circle-plus-outline" @click="Supplement" :disabled="isShow || isRegular">补单</el-button>
      <el-checkbox v-model="twoInfo" label="二维" border style="float:right;heigth:30px"></el-checkbox>
    </div>
    <div class="od-tab">
      <div class="tab-left">
        <el-table 
        border=""
          ref="table1" 
          :data="tableData1" 
          style="width: 100%" 
          highlight-current-row 
          @row-click="rowClick"
          @row-dblclick="dbClick"
          :header-cell-style="{background:'#f4f4f4',padding:0,height:'40px'}" 
          :cell-style="{padding:0,height:'26px'}"
          height="74vh" >
          <!-- <el-table-column
            type="index"
            label="序号"
            width="50">
          </el-table-column> -->
          <el-table-column prop="outSequence" label="退货序号" width="140" show-overflow-tooltip></el-table-column>
          <el-table-column prop="sendTime" label="寄货日期" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column prop="affirm" label="已确认" width="80" show-overflow-tooltip></el-table-column>
          <el-table-column prop="mnemonicCode" label="收货" width="80" show-overflow-tooltip></el-table-column>
          <el-table-column prop="enteringTime" label="日期" width="120" show-overflow-tooltip></el-table-column>
          <el-table-column prop="number" label="数量" width="90" show-overflow-tooltip></el-table-column>
          <el-table-column prop="money" label="金额" width="90" show-overflow-tooltip></el-table-column>
          <el-table-column prop="customerName" label="操作员" width="120" show-overflow-tooltip></el-table-column>
          <el-table-column prop="remark" label="备注" width="100" show-overflow-tooltip></el-table-column>
        </el-table>
      </div>
      <div class="tab-right">
        <el-table 
        border=""
          id="outTable"
          :data="tableData2" 
          style="width: 100%" 
          :header-cell-style="{background:'#f4f4f4',padding:0,height:'40px'}" 
          :cell-style="{padding:0,height:'26px'}"
          @row-click="rowClick1"
          height="74vh">
          <!-- <el-table-column
            type="index"
            label="序号"
            width="50">
          </el-table-column> -->
          <template v-for="i in this.hList">
            <el-table-column 
              :prop="i.prop" 
              :label="i.label" 
              :width="i.width" 
              :key="i.prop" >
            </el-table-column>
          </template>
        </el-table>
      </div>
    </div>
    <foot-view class="footView">
      <div slot="text">
        <span>数据库：{{jxQuarter}}</span>
      </div>
    </foot-view>

    <new-vs 
      v-show="showNew"
      :flag="showNew"
      :title="title"
      :detail="detail"
      :detail2="detail2"
      @success1="success1"
      @inpNumChange="()=>{this.inpList = this.getInput()}"
      @sendXH="sendXH"
      @closeNew="closeNew"
      @saveNew="saveNew">
    </new-vs>
  </div>
</template>

<script>
import { confirm, orderQuery } from '../../../network/manufacturerOrder'
import {getGoods,deleteGoods,returnConfirm} from '../../../network/shop'
import footView from '../../../components/FootView'
import newVs from './childComp/newVs'
import enterUp from '../../../components/mixins/enterUp'

export default {
  name: 'store_return_s',
  mixins:[enterUp],
  created(){
    let jxQuarterVal = document.cookie.match(RegExp('jxQuarter' + '=([^;]+)'))
    this.jxQuarter = jxQuarterVal ? jxQuarterVal[1] : null
    
    this.inpList = this.getInput()
    if(localStorage.getItem('whatState') == '直营'){
      this.isRegular = false
    }else{
      this.isRegular = true
    }
    this.hList = this.hList1
    // this._getGoods()
    var day = new Date()
    var date1 = day.getFullYear()
    var date2 = (day.getMonth()+1) < 10 ? `0${day.getMonth()+1}` : (day.getMonth()+1)
    var date3 = day.getDate() < 10 ? `0${day.getDate()}` : day.getDate()
    this.now = date1 + '-' + date2 + '-' +date3
  },
  data(){
    return {
      inpList:[],
      isRegular:false,
      isShow:false,
      jxQuarter:null,
      // jxQuarter: document.cookie.match(new RegExp("jxQuarter" + name + "=([^;]*)(;|$)"))[1],
      saveList:[],
      title:'',
      sequence:'',
      showNew: false,
      twoInfo: false,
      hList: [],
      hList1:[
        {prop:'kindsNumber',label:'款号',width:'120'},
        {prop:'colorName',label:'颜色',width:''},
        {prop:'size',label:'尺码编号',width:''},
        {prop:'sum',label:'数量',width:''},
        {prop:'sellingPrice',label:'售价',width:'100'},
        {prop:'discount',label:'折数',width:'50'},
        {prop:'onePrice',label:'单价',width:'100'},
        {prop:'money',label:'金额',width:'80'},
        {prop:'brandedName',label:'品名',width:'120'},
        {prop:'commodityName',label:'品牌',width:'120'},
        {prop:'entryTime',label:'录入时间',width:'160'}
      ],
      hList2: [
        {prop:'typeNumber',label:'型号',width:''},
        {prop:'colorName',label:'颜色',width:''},
        {prop:'c05',label:'05-XS',width:''},
        {prop:'c07',label:'07-S',width:''},
        {prop:'c09',label:'09-M',width:''},
        {prop:'c11',label:'11-L',width:''},
        {prop:'c13',label:'13-XL',width:''},
        {prop:'c15',label:'15-2L',width:''},
        {prop:'c17',label:'17-3L',width:''},
        {prop:'c19',label:'19-4L',width:''},
        {prop:'c21',label:'21-5L',width:''},
        {prop:'f',label:'F',width:''},
        {prop:'total',label:'小计',width:''},
        {prop:'sellingPrice',label:'售价',width:''},
        {prop:'onePrice',label:'单价',width:''},
        {prop:'money',label:'金额',width:''},
        {prop:'brandedName',label:'品名',width:'120'},
        {prop:'commodityName',label:'品牌',width:'120'},
        {prop:'',label:'季度',width:'120'},
      ],

      tableData1: [],
      tableData2: [],

      kindsNumberList: [],
      styleNumberList: [],
      detail:[],
      detail2:[],
      time:'',
      now:'',
      twoList:[],
    }
  },
  watch: {
    twoInfo(a,b){
      if (this.twoInfo == true) {
        this.hList = this.hList2
        if(this.tableData2 != ''){
          var nList = []
          for(let c of this.twoList){
            if(this.sequence == c.outSequence){
              
              nList.push(c)
            }
            this.tableData2 = nList
          }
        }
      } else {
        this.hList = this.hList1
        if(this.tableData2 != ''){
          var mList = []
          for(let c of this.saveList){
            if(this.sequence == c.sequence){
              mList.push(c)
            }
            this.tableData2 = mList
          }
        }
      }
    }
  },
  methods: {
    closeNew(){
      this.showNew=false
      this._getGoods()
    },
    sendXH(data){
      console.log(data)
      if(data != ''){
        this.sequence = data
      }
      console.log(this.sequence)
      this.showNew = false
      this._getGoods()
    },
    //确认
    Confirm(){
      if(this.sequence == ''){
        this.$message.warning({message:'请选择一条数据确认',offset:400})
        return
      }
      returnConfirm({outSequence:this.sequence}).then(res=>{
        console.log(res.data)
        if(res.data.code == 200){
          this.$message.success({message:'确认成功',offset:400})
          this._getGoods()
        }
      })
    },
    success1(data){
      console.log(data)
      this.sequence = data[0].outSequence
      for(let a of data){
        if(a.affirm == 1){a.affirm = '已确认'}else{a.affirm = '未确认'}
        if(a.mnemonicCode == 1){a.mnemonicCode = '已收货'}else{a.mnemonicCode = '未收货'}
      }
      this.tableData1 = data
      this._getGoods()
    },
    Supplement(){
      this.showNew=true,
      this.title='补单'
    },
    dbClick(e){
      // console.log(e)
      // if(this.now == this.time){
        if(localStorage.getItem('whatState') != '直营'){
          this.$message('非直营客户不可更改数据')
          return
        }
        
        if(e.affirm != '已确认'){
          this.showNew=true
          this.title = '编辑'
          this.detail = [e]
          this.detail2 = []
          for(let a of this.tableData2){
            if(e.outSequence == a.sequence){
              this.detail2.push(a)
            }
          }
        }else{
          this.$message.warning({message:'此单已确认'})
          return
        }
      // }else{
      //   this.$message.warning({message:'只能修改当天数据',offset:400})
      // }
      
    },
    _getGoods(){
      //退货查询
      if(this.sequence == ''){return}
      getGoods({outSequence:this.sequence}).then(res=>{
        console.log(res.data)
        if(res.data.data != null){
          for(let w of res.data.data.outInquireTotalList){
            if(w.mnemonicCode == 1){w.mnemonicCode = '已收货'}else{w.mnemonicCode = '未收货'}
            if(w.affirm == 1){w.affirm = '已确认'}else{w.affirm = '未确认'}
          }
          this.tableData1 = res.data.data.outInquireTotalList
          this.saveList = res.data.data.outInquireInfoList
          this.twoList = res.data.data.customerOutInfoTwos
        }else{
          this.tableData1 = []
          this.saveList = []
          this.twoList = []
          this.tableData2 = []
        }
      })
    },
    rowClick(e){
      console.log(e)
      if(e.affirm == '已确认'){this.isShow = true}else{this.isShow = false}
      this.sequence = e.outSequence
      this.time = e.enteringTime.slice(0,10)
      this.detail = [e]
      this.detail2 = []
      this.tableData2 = []

      if(this.twoInfo == false){
        for(let a of this.saveList){
          if(e.outSequence == a.sequence){
            this.tableData2.push(a)
            this.detail2.push(a)
          }
        }
      }

      if(this.twoInfo == true){
        this.twoList.forEach(el=>{
          if(e.outSequence == el.sequence){
            this.tableData2.push(el)
          }
        })
      };

      // for(let c of this.tableData2){
      //     if(e.outSequence == c.sequence){
      //       this.detail2.push(c)
      //     }
      //   }
      console.log(this.detail2)
    },
    rowClick1(e){
      console.log(e)
    },
    //删除
    Delete(){
      if(this.sequence == ''){
        this.$message.warning({message:'请选择一条数据删除'})
        return
      }
      // if(this.now != this.time){
      //   this.$message.warning({message:'只能删除当天数据'})
      //   return
      // }
      this.$confirm('是否删除本条的数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteGoods({outSequence:this.sequence}).then(res=>{
          console.log(res.data)
          if(res.data.code == 200){
            this.$message.success({message:'删除成功'})
            this._getGoods()
          }else{
            this.$message.error({message:'删除失败'})
          }
        })
      })
    },

    print(){

    },
    saveNew(a){},
    conversionList(list){
      this.styleNumberList = []
      let sn = 0
      let cn = 0

      // 款号颜色
      for (const i of list) {
        if (!this.styleNumberList.length) {
          // 空数组直接添加
          this.styleNumberList.push({
            styleNumber: i.styleNumber,
            colorNo: i.colorNo,
            colorName: i.colorName,
            c05: 0,
            c07: 0,
            c09: 0,
            c11: 0,
            c13: 0,
            c15: 0,
            c17: 0,
            c19: 0,
            c21: 0,
            c23: 0,
            f: 0,
            total: 0,
            sellingPrice: i.sellingPrice,
            onePrice: i.onePrice,
            commodityName: i.commodityName,
            brandedName: i.brandedName,
            quarter: i.quarter,
            tagPrice: i.sellingPrice
          })
          continue
        }
        for (const j of this.styleNumberList) {
          if (j.styleNumber == i.styleNumber) {
            sn = 1
            // 货号存在，判断颜色有没有
            for (const k of this.styleNumberList) {
              if (i.styleNumber == k.styleNumber && i.colorName == k.colorName) {
                cn = 1
                break
              }
              cn = 0
            }
            if(cn) break
          }
          sn = 0
        }
        
        if (!sn || !cn) {
          // 货号或当前货号的颜色不存在，直接添加
          this.styleNumberList.push({
            styleNumber: i.styleNumber,
            colorNo: i.colorNo,
            colorName: i.colorName,
            c05: 0,
            c07: 0,
            c09: 0,
            c11: 0,
            c13: 0,
            c15: 0,
            c17: 0,
            c19: 0,
            c21: 0,
            c23: 0,
            f: 0,
            total: 0,
            sellingPrice: i.sellingPrice,
            onePrice: i.onePrice,
            commodityName: i.commodityName,
            brandedName: i.brandedName,
            quarter: i.quarter,
            tagPrice: i.sellingPrice
          })
        }
      }

      // 数量赋值
      list.forEach(e => {
        for (const i of this.styleNumberList) {
          if (e.styleNumber == i.styleNumber && e.colorName == i.colorName ) {
            i[e.relevanceSize] = i[e.relevanceSize]+e.number
            break
          }
        }
      })

      // 计算总数
      this.styleNumberList.forEach(e => {
        e.total = e.c05 + e.c07 + e.c09 + e.c11 + e.c13 + e.c15 + e.c17 + e.c19 + e.c21 + e.c23 + e.f
        e.sellingPriceMoney = e.sellingPrice*e.total
        e.money = e.onePrice*e.total
        if (e.c05 === 0) e.c05 = ''
        if (e.c07 === 0) e.c07 = ''
        if (e.c09 === 0) e.c09 = ''
        if (e.c11 === 0) e.c11 = ''
        if (e.c13 === 0) e.c13 = ''
        if (e.c15 === 0) e.c15 = ''
        if (e.c17 === 0) e.c17 = ''
        if (e.c19 === 0) e.c19 = ''
        if (e.c21 === 0) e.c21 = ''
        if (e.c23 === 0) e.c23 = ''
        if (e.f === 0) e.f = ''
      })
    }
  },
  components:{
    footView,
    newVs
  },
}
</script>